package view
{
	import data.Globals;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	
	import manager.EffectName;
	import manager.MapConfig;
	import manager.SoundManage;
	import manager.TankName;
	
	import view.base.ClearView;
	import view.map.Grad;
	import view.other.ShanghaiWall;
	import view.other.YaLuJiView;
	
	public class GradMap extends ClearView
	{
		private var gradMap:Object;
	
		public function GradMap()
		{
			super();
			gradMap = {};
			initGrad();
			this.addEventListener(MouseEvent.MOUSE_UP,sitDown);
			this.addEventListener(MouseEvent.MOUSE_OVER,over);
		}
		
		public function setGradIsCanSite(_x:int,_y:int,boo:Boolean):void{
			var str:String = (_x-Globals.GRAD_WIDTH/2)+"_"+(_y - Globals.GRAD_HEIGHT/2);
			if(gradMap[str]){
				(gradMap[str] as Grad).isCanSit = boo;
			}
		}
		
		private function over(event:MouseEvent):void
		{
			var grad:Grad = event.target as Grad;
			if(grad.isCanSit){
				if(Globals.gameView.getCurArmType()){
					Globals.gameView.showMouseEffect(grad.x,grad.y);
				}
			}
		}
		/**
		 * 如果当前点击的grad可以坐下 放下炮塔
		 * @param event
		 * 
		 */		
		private function sitDown(event:MouseEvent):void
		{
			var grad:Grad = event.target as Grad;
			var p:Point = new Point(grad.x+Globals.GRAD_WIDTH/2,grad.y+Globals.GRAD_HEIGHT/2);
			if(grad.isCanSit){
				if(Globals.gameView.getCurArmType()&&Globals.gameView.getCurArmType()!=TankName.YALUJI){
					Globals.gameView.produceTank(Globals.gameView.getCurArmType(),p);
					grad.isCanSit = false;
					grad.name = Globals.gameView.getCurArmType();
					grad.filters = null;
					grad.alpha = 0;
					SoundManage.getInstance().play(SoundManage.SITE_ARMS);
				}
			}
		}
		
		private function initGrad():void
		{
			//行
			var col:int = Math.ceil(Globals.stage_height/Globals.GRAD_HEIGHT);
			//列
			var row:int = Math.ceil(Globals.stage_width/Globals.GRAD_WIDTH);
			for(var i:int=0;i<row;i++){
				for(var j:int=0;j<col;j++){
					var grad:Grad = new Grad();
					grad.graphics.lineStyle(2,0x000000);
					grad.graphics.beginFill(0xffff00);
					grad.graphics.drawRect(0,0,Globals.GRAD_WIDTH - 2,Globals.GRAD_HEIGHT-2);
					grad.graphics.endFill();
					grad.x = i*Globals.GRAD_WIDTH;
					grad.y = j*Globals.GRAD_HEIGHT;
					this.addChild(grad); 
					gradMap[grad.x+"_"+grad.y] = grad;
					grad.alpha = 0;
				}
			}
		}
	
		public function drawLine():void
		{
			clear();
			var line:Array = MapConfig.getInstance().line;
			var i:int=0;
			var p:Point;
			var n:String;
			var grad:Grad ;
			for(i;i<line.length;i++){
			    p = line[i];
				n = (p.x-Globals.GRAD_WIDTH/2) + "_" + (p.y-Globals.GRAD_HEIGHT/2);
				grad = gradMap[n];
				grad.graphics.clear();
				grad.graphics.lineStyle(2,0xffffff);
				grad.graphics.beginFill(0x00ff00);
				grad.graphics.drawRect(0,0,Globals.GRAD_WIDTH - 2,Globals.GRAD_HEIGHT-2);
				grad.graphics.endFill();
				grad.isCanSit = false;
			}
		    //设置障碍点
			line = MapConfig.getInstance().barrier.concat(MapConfig.getInstance().commonBarrier);
			i=0;
			for(i;i<line.length;i++){
				p = line[i];
				n = (p.x-Globals.GRAD_WIDTH/2) + "_" + (p.y-Globals.GRAD_HEIGHT/2);
				grad = gradMap[n];
				grad.graphics.clear();
				grad.graphics.lineStyle(2,0xffffff);
				grad.graphics.beginFill(0xffff00);
				grad.graphics.drawRect(0,0,Globals.GRAD_WIDTH - 2,Globals.GRAD_HEIGHT-2);
				grad.graphics.endFill();
				grad.isCanSit = false;
			}
		}

		private function clear():void
		{
			for(var key:String in gradMap){
				var grad:Grad = gradMap[key];
				grad.graphics.clear();
				grad.graphics.lineStyle(2,0x000000);
				grad.graphics.beginFill(0xff0000);
				grad.graphics.drawRect(0,0,Globals.GRAD_WIDTH - 2,Globals.GRAD_HEIGHT-2);
				grad.graphics.endFill();
				grad.isCanSit = true;
			}
		}
	}
}